/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkUtil/MkQueue.h"
#include "MkUtil/MkThread.h"
#include <gtest/gtest.h>

typedef struct _mk_test_struct
{
    Uint32      bBreak;
    MkString    strTmp;
}MkTestStruct;

TEST(MkQueueTest, QueueNoWait)
{
    CMkQueue<MkTestStruct> testQueue(1);
    Uint32 ErrorCode = NoneError;

    MkTestStruct tmpStruct;
    tmpStruct.bBreak = 1;
    tmpStruct.strTmp = "abc";

    MkTestStruct OutStruct;
    OutStruct.bBreak = 0;
    OutStruct.strTmp = "123";
    ;
    ErrorCode = testQueue.Input(tmpStruct);
    EXPECT_EQ(NoneError, ErrorCode);
    ErrorCode = testQueue.Output(OutStruct);
    EXPECT_EQ(NoneError, ErrorCode);
    EXPECT_EQ(tmpStruct.bBreak, OutStruct.bBreak);
    EXPECT_STREQ(tmpStruct.strTmp.c_str(), OutStruct.strTmp.c_str());

    ErrorCode = testQueue.Input(tmpStruct);
    EXPECT_EQ(NoneError, ErrorCode);

    //input failed
    ErrorCode = testQueue.Input(tmpStruct);
    EXPECT_NE(NoneError, ErrorCode);

    testQueue.Clear();
    BOOL bEmpty = testQueue.Empty();
    EXPECT_EQ(TRUE, bEmpty);
}

TEST(MkQueueTest, QueueWait)
{
    Uint32 ErrorCode = NoneError;
    CMkQueue<MkTestStruct> testQueue(2);
    MkTestStruct OutStruct;
    ErrorCode = testQueue.Output(OutStruct, 10);
    EXPECT_NE(NoneError, ErrorCode);

    ErrorCode = testQueue.Output(OutStruct);
    EXPECT_NE(NoneError, ErrorCode);

    MkTestStruct tmpStruct;
    tmpStruct.bBreak = 0;
    tmpStruct.strTmp = "abc";
    ErrorCode = testQueue.Input(tmpStruct, 0xFFFFFF);
    EXPECT_EQ(NoneError, ErrorCode);

    tmpStruct.bBreak = 1;
    tmpStruct.strTmp = "abc";
    ErrorCode = testQueue.Input(tmpStruct);

    tmpStruct.bBreak = 2;
    tmpStruct.strTmp = "abc";
    ErrorCode = testQueue.Input(tmpStruct);
    EXPECT_NE(NoneError, ErrorCode);

    tmpStruct.bBreak = 2;
    tmpStruct.strTmp = "abc";
    ErrorCode = testQueue.Input(tmpStruct,10);
    EXPECT_NE(NoneError, ErrorCode);

    OutStruct.bBreak = 0;
    OutStruct.strTmp = "123";
    while (1) {
        testQueue.Output(OutStruct, 0xFFFFFF);
        if (OutStruct.bBreak) {
            break;
        }
    }
}

void FreeNodeFun(MkTestStruct& Tmp, void* pUser)
{

}

TEST(MkQueueTest, QueueClear)
{
    CMkQueue<MkTestStruct> testQueue(2);
    testQueue.SetFreeFun(FreeNodeFun, nullptr);
    MkTestStruct tmpStruct;
    tmpStruct.bBreak = 0;
    tmpStruct.strTmp = "abc";
    Uint32 ErrorCode = testQueue.Input(tmpStruct, 0xFFFFFF);
    EXPECT_EQ(NoneError, ErrorCode);

    tmpStruct.bBreak = 1;
    tmpStruct.strTmp = "abc";
    ErrorCode = testQueue.Input(tmpStruct, 0xFFFFFF);
    EXPECT_EQ(NoneError, ErrorCode);

    testQueue.Clear();

    MkTestStruct OutStruct;
    ErrorCode = testQueue.Output(OutStruct, 0xFFFFFF);
    EXPECT_NE(NoneError, ErrorCode);

    tmpStruct.bBreak = 1;
    tmpStruct.strTmp = "abc";
    ErrorCode = testQueue.Input(tmpStruct, 0xFFFFFF);
    EXPECT_NE(NoneError, ErrorCode);
}